package com.spukmk3me.scene;

/* $if SPUKMK3ME_DEBUG$ */
import com.spukmk3me.debug.Logger;
/* $endif$ */
import com.spukmk3me.resource.Resource;

public abstract class Entity
{
    /**
     *  For entity with multiple resource dependency.
     *  @param resourceList List of resources.
     */
    protected Entity( Resource[] resourceList )
    {
        if ( resourceList != null )
        {
            int nNonNullResource = 0;
            
            for ( int i = 0; i != resourceList.length; ++i )
            {
                if ( resourceList[ i ] != null )
                    ++nNonNullResource;
            }
            
            if ( nNonNullResource != 0 )
            {
                m_resourceList = new Resource[ nNonNullResource ];
                
                int index = 0;
                
                for ( int i = 0; i != resourceList.length; ++i )
                {
                    if ( resourceList[ i ] != null )
                        m_resourceList[ index++ ] = resourceList[ i ];
                }
            }
            else
                m_resourceList = null;
        }
        else
            m_resourceList = null;
    }
    
    /**
     *  For entity with single resource dependency.
     *  @param resource Resource.
     */
    protected Entity( Resource resource )
    {
        if ( resource != null )
        {
            m_resourceList = new Resource[ 1 ];
            m_resourceList[ 0 ] = resource;
        }
        else
            m_resourceList = null;
    }
    
    final void incResourceCounter()
    {
        if ( m_resourceList != null )
        {
            for ( int i = 0; i != m_resourceList.length; ++i )
                m_resourceList[ i ].inc();
        }
    }
    
    final void decResourceCounter()
    {
        if ( m_resourceList != null )
        {
            for ( int i = 0; i != m_resourceList.length; ++i )
                m_resourceList[ i ].dec();
        }
    }

    public abstract byte getType();
    public abstract Entity duplicate();
    public abstract int getWidth();
    public abstract int getHeight();
    
    /**
     *  A entity is timeless if and only if the display of that entity isn't changed over time.
     *  \details An example of timeless entity is image.
     *  \details An example of non-timeless entity is sprite. 
     *  @return
     */
    public abstract boolean isTimeless();
    
    public static final byte ET_IMAGE   = 0;
    public static final byte ET_SPRITE  = 1;
    public static final byte ET_TEXT    = 2;
    public static final byte ET_TILEMAP = 3;
    public static final byte ET_POLYGON = 4;
    
    private final Resource[]  m_resourceList;
}
